<template>
  <div class="page-order">
    <div class="form">
      <div class="demo-input-suffix">
        <div class="top">
          <!-- 查询表单 -->
          <el-form inline :model="formData" size="mini">
            <!-- 订单号-->
            <el-form-item label="订单号">
              <el-input placeholder="订单号" v-model="formData.orderNo"></el-input>
            </el-form-item>
            <!-- 收件人="" -->
            <el-form-item label="收件人">
              <el-input placeholder="收件人" v-model="formData.consignee">
              </el-input>
            </el-form-item>
            <!-- 手机号-->
            <el-form-item label="手机号">
              <el-input placeholder="手机号" v-model="formData.phone"></el-input>
            </el-form-item>
            <!-- 订单状态 -->
            <el-form-item label="订单状态">
              <el-select placeholder="订单状态" v-model="formData.orderState">
                <el-option label="已受理" value="已受理"></el-option>
                <el-option label="派送中" value="派送中"></el-option>
                <el-option label="已完成" value="已完成"></el-option>
              </el-select>
            </el-form-item>

            <!-- 时间日期选择器 -->
            <!-- value-format 值格式 -->
            <!-- format 显示格式 -->
            <el-form-item label="选择时间" class="demonstration">
              <el-date-picker v-model="formData.date"
                value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange"
                :picker-options="pickerOptions" range-separator="至"
                start-placeholder="开始日期" end-placeholder="结束日期" align="right">
              </el-date-picker>
            </el-form-item>

            <!-- 查询 -->
            <el-form-item>
              <el-button type="primary" @click="search">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- 订单数据表格 -->
        <div class="content mt20">
          <el-table :data="tableData" border :style="{width: w + 'px'}">
            <el-table-column fixed prop="orderNo" label="订单号"></el-table-column>
            <el-table-column prop="orderTime" label="下单时间"></el-table-column>
            <el-table-column prop="phone" label="手机号"></el-table-column>
            <el-table-column prop="consignee" label="收货人"></el-table-column>
            <el-table-column prop="deliverAddress" label="配送地址">
            </el-table-column>
            <el-table-column prop="deliveryTime" label="送达时间"></el-table-column>
            <el-table-column prop="userNote" label="用户备注"></el-table-column>
            <el-table-column prop="orderAmount" label="订单金额"></el-table-column>
            <el-table-column prop="orderState" label="订单状态"></el-table-column>

            <!-- 操作 -->
            <el-table-column fixed="right" label="操作" width="100">
              <template slot-scope="scope">
                <el-button @click="handleClick(scope.row)" type="text"
                  size="small">
                  查看
                </el-button>
                <el-button type="text" size="small"
                  @click="editOrder(scope.row)">
                  编辑
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->

        <el-pagination class="mt20" @current-change="handleCurrentChange"
          :current-page="currentPage" :page-size="pageSize" :page-sizes="[3, 5]"
          background layout="total, sizes, prev, pager, next, jumper"
          :total="total"></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { getOrderListReq } from "@/api/order";
import resizeTable from "@/mixins/resizeTable.js";
export default {
  mixins: [resizeTable],
  data() {
    return {
      w: 0,
      //表单数据
      formData: {
        orderNo: "",
        consignee: "",
        phone: "",
        orderState: "",
        date: [],
      },
      // 分页
      currentPage: 1,
      pageSize: 3,
      total: 0,
      //查询表单
      searchForm: {},

      // 时间数据
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      value1: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
      value2: "",
      // 表格数据
      tableData: [],
      //商品数据唯一id
    };
  },
  methods: {
    async getData() {
      let currentPage = this.currentPage;
      let pageSize = this.pageSize;
      let date = JSON.stringify(this.searchForm.date || "");
      console.log('date :>> ', date)
      let res = await getOrderListReq({
        currentPage,
        pageSize,
        ...this.searchForm,
        date,
      });
      // console.log('res :>> ', res)//有数据
      let { data, total } = res.data;
      this.tableData = data;
      this.total = total;
      //如果获取data为[] 且currentPage>1 退回上一页
      if (data.length === 0 && this.currentPage !== 1) {
        this.currentPage--;
        this.getData();
      }
    },

    //查看详情
    handleClick(row) {
      // console.log(row)
      // let id = row.id;
      // console.log('id :>> ', id);
      this.$router.push({ path: "/order/order-look", query: { id: row.id } });
    },
    //编辑
    editOrder(row) {
      // console.log("row :>> ", row);
      let id=row.id
      // console.log('id :>> ', id);
      this.$router.push(`/order/order-edit/${id}`)
    },

    //查询---双向数据绑定
    search() {
      this.currentPage = 1;
      this.searchForm = { ...this.formData };
      this.getData();
    },

    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      this.currentPage = val;
      this.getData();
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="scss" scoped>
// .page-order {
//   width: 100%;
// }
// .el-input__inner {
//   width: 300px;
// }
</style>
